package com.probox.common.entity.service.order.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.entity.domain.customer.CustomerInvoice;
import com.probox.common.entity.service.customer.ICustomerInvoiceService;
import com.probox.order.api.domain.OrderInvoiceVo;
import com.probox.order.api.domain.OrderQuery;
import com.probox.common.entity.domain.order.OrderInfo;
import com.probox.common.entity.service.order.IOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.order.OrderInvoiceMapper;
import com.probox.common.entity.domain.order.OrderInvoice;
import com.probox.common.entity.service.order.IOrderInvoiceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 开票信息Service业务层处理
 *
 * @author hds
 * @date 2021-06-07
 */
@Service
@Transactional
public class OrderInvoiceServiceImpl extends ServiceImpl<OrderInvoiceMapper, OrderInvoice> implements IOrderInvoiceService {
    @Autowired
    private OrderInvoiceMapper orderInvoiceMapper;

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private ICustomerInvoiceService customerInvoiceService;

    /**
     * 查询开票信息
     *
     * @param recordUid 开票信息ID
     * @return 开票信息
     */
    @Override
    public OrderInvoice selectOrderInvoiceById(Long recordUid) {
        return this.getById(recordUid);
    }

    /**
     * 查询开票信息列表
     *
     * @param orderInvoice 开票信息
     * @return 开票信息
     */
    @Override
    public List<OrderInvoice> queryList(OrderInvoice orderInvoice) {
        List<Long> idList = null;
        if (ObjectUtil.isNotEmpty(orderInvoice.getOrderNo())) {
            OrderQuery query = new OrderQuery();
            query.setStoreId(SecurityUtils.getStoreId());
            query.setOrderNo(orderInvoice.getOrderNo());
            query.setPlatformUid(SecurityUtils.getPlatformId());
            idList = orderInfoService.getOrderUidByNo(query);
        }

        QueryWrapper<OrderInvoice> queryWrapper = Wrappers.query();
        if (ObjectUtil.isNotEmpty(idList)) {
            queryWrapper.in("ORDER_UID", idList);
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getRecordUid())) && orderInvoice.getRecordUid() != null) {
            queryWrapper.eq("RECORD_UID", orderInvoice.getRecordUid());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getInvoiceNo())) && orderInvoice.getInvoiceNo() != null) {
            queryWrapper.eq("INVOICE_NO", orderInvoice.getInvoiceNo());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getInvoiceCode())) && orderInvoice.getInvoiceCode() != null) {
            queryWrapper.eq("INVOICE_CODE", orderInvoice.getInvoiceCode());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getInvoiceUrl())) && orderInvoice.getInvoiceUrl() != null) {
            queryWrapper.eq("INVOICE_URL", orderInvoice.getInvoiceUrl());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getSellerName())) && orderInvoice.getSellerName() != null) {
            queryWrapper.eq("SELLER_NAME", orderInvoice.getSellerName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getDeliverMethod())) && orderInvoice.getDeliverMethod() != null) {
            queryWrapper.eq("DELIVER_METHOD", orderInvoice.getDeliverMethod());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getUpdateUser())) && orderInvoice.getUpdateUser() != null) {
            queryWrapper.eq("UPDATE_USER", orderInvoice.getUpdateUser());
        }
        if (StringUtils.isNotEmpty(String.valueOf(orderInvoice.getUpdateName())) && orderInvoice.getUpdateName() != null) {
            queryWrapper.eq("UPDATE_NAME", orderInvoice.getUpdateName());
        }

        queryWrapper.orderByDesc("UPDATE_TIME");

        queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
        queryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());

        List<OrderInvoice> invoiceList = orderInvoiceMapper.selectList(queryWrapper);
        if (ObjectUtil.isNotEmpty(invoiceList)) {
            for (OrderInvoice invoice : invoiceList) {
                if (ObjectUtil.isNotEmpty(invoice.getOrderUid())) {
                    OrderInfo orderInfo = orderInfoService.getById(invoice.getOrderUid());
                    if (orderInfo != null)
                        invoice.setOrderNo(orderInfo.getOrderNo());
                }
            }
        }
        return invoiceList;
    }

    /**
     * 新增开票信息
     *
     * @param orderInvoice 开票信息
     * @return 结果
     */
    @Override
    public boolean insertOrderInvoice(OrderInvoice orderInvoice) {
        return this.save(orderInvoice);

    }


    /**
     * 开票信息
     *
     * @param orderInvoice
     * @return
     */
    @Override
    public boolean addInvoice(OrderInvoice orderInvoice) {
        /**
         * 查询客户发票信息
         */
        CustomerInvoice byId = customerInvoiceService.getById(orderInvoice.getInvoiceId());
        if (ObjectUtil.isEmpty(byId)) {
            throw new BaseException("客户发票信息不存在");
        }
        orderInvoice.setPlatformUid(SecurityUtils.getPlatformId());
        orderInvoice.setSellerName(byId.getCompanyName());
        orderInvoice.setDeliverMethod(byId.getCarrierType());
        orderInvoice.setDeliverPhone(byId.getReceiverPhone());
        orderInvoice.setDeliverEmail(byId.getReceiverEmail());
        orderInvoice.setDataStatus(DataStatus.valid.getKey());
        orderInvoice.setUpdateName(SecurityUtils.getUsername());
        orderInvoice.setUpdateUser(SecurityUtils.getUserId());
        orderInvoice.setUpdateTime(DateUtil.date());
        if (this.save(orderInvoice)) {
            OrderInfo orderInfo = new OrderInfo();
            orderInfo.setOrderUid(orderInvoice.getOrderUid());
            orderInfo.setInvoiceStatus(DataStatus.valid.getKey());
            orderInfoService.updateById(orderInfo);
        }
        return true;
    }

    /**
     * 修改开票信息
     *
     * @param orderInvoice 开票信息
     * @return 结果
     */
    @Override
    public boolean updateOrderInvoice(OrderInvoice orderInvoice) {

        return this.updateById(orderInvoice);
    }

    /**
     * 批量删除开票信息
     *
     * @param recordUids 需要删除的开票信息ID
     * @return 结果
     */
    @Override
    public boolean deleteOrderInvoiceByIds(Long[] recordUids) {
        UpdateWrapper<OrderInvoice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("GIFT_UID", recordUids);
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_USER", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    /**
     * 根据ID获取开票信息
     *
     * @param orderUid
     * @return
     */
    @Override
    public OrderInvoiceVo getByOrderUid(Long orderUid) {
        return orderInvoiceMapper.getByOrderUid(orderUid);
    }


}
